package com.shijie.controller.app;

import com.shijie.component.AppUserService;
import com.shijie.core.ServiceException;
import com.shijie.domain.vo.UserPayInformation;
import com.shijie.service.UserOrdersService;
import com.shijie.domain.entity.UserOrders;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*
 *  用户订单等相关接口
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/orders")
public class AppUserOrdersController {

    @Resource
	private UserOrdersService userOrdersService;

    @Resource
    private AppUserService appUserService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestParam(required = false) Integer entityType, @RequestParam(required = false) Integer status) {
        PageHelper.startPage(page, pageSize);
        UserOrders qCondition = new UserOrders();
        qCondition.setBuyerId(appUserService.getCurrentUser().getId());
        qCondition.setEntityType(entityType);
        if(status>0){
        	qCondition.setStatus(status);	
        }
        List<UserOrders> list = userOrdersService.selectOrdersForUser(qCondition);
        PageInfo<UserOrders> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }

    /**
     * entityType 必传  1 线下活动 4 商品  6 团购；
     * entityId 对应的活动ID，商品ID 必填
     *
     *          PRODUCT 时 list：skuid ，totalNum 必传
     *          ACTIVITY 时 list : 报名只会有一条数据
     *
     * @param userPayInformation
     * @return 预支付单的流水号，之后请求微信支付接口
     */
	@PostMapping
    public Result add(@RequestBody UserPayInformation userPayInformation) {
        try {
            return ResultGenerator.success().putData("Serial", userOrdersService.saveOrders(userPayInformation, appUserService.getCurrentUser()));
        } catch (ServiceException e) {
            e.printStackTrace();
            ResultGenerator.error(e.getMessage());
        }
        return ResultGenerator.error("创建订单失败！");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userOrdersService.deleteById(id);
        return ResultGenerator.success();
	}

    @PutMapping
    public Result update(@RequestBody UserOrders userOrders) {
        userOrdersService.updateById(userOrders);
        return ResultGenerator.success();
	}

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        UserOrders userOrders = userOrdersService.selectById(id);
        return ResultGenerator.success(userOrders);
	}

    /**
     * 用户中心用户订单角标
     *
     * @return
     */
    @GetMapping("/cornerMark")
    public Result cornerMark() {
        Map<String, Integer> map = userOrdersService.selectCornerMark(appUserService.getCurrentUser().getId());
        return ResultGenerator.success(map);
    }
}

